home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / Demos_Files / DLL / ADPTSTEP.C next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  6.9 KB  |  210 lines

  1. // Implementation of NeuroSolutions Momentum component with adaptive step
  2. // sizes and postprocessor for error control
  3. //
  4. // Version 2.0, 17/10/96
  5. //
  6. // Programmed by Alexandre Bernardino
  7. // IST, Lisbon, Portugal
  8.  
  9. #include "NSDLL.h"
  10. #include <windows.h>
  11. #include <limits.h>
  12.  
  13.  
  14. /* GLOBAL VARIABLES */
  15.  
  16. int gEpoch=-1;
  17. NSFloat bestError;
  18. int ERR_GROW_MORE_THAN_ALLOWED = 0;
  19. int ERROR_DECREASED = 0;
  20.  
  21. /*****************************/
  22. /* Gradient search procedure */
  23.  
  24. __declspec(dllexport) void performMomentum(
  25.     DLLData    *instance,    // Pointer to instance data (may be NULL)
  26.     NSFloat    *weights,    // Pointer to the vector of weights
  27.     int    length,        // Length of the weight vector
  28.     NSFloat    *gradient,    // Pointer to the vector of gradients, one for each weight
  29.     NSFloat    *step,        // Pointer to the learning rate/s
  30.     BOOL    individual,    // Indicates whether their is one learning rate for all weights (FALSE),
  31.                 // or each weight has its own learning rate
  32.     int        stepDivisor,// The number each step size should be divided by
  33.     BOOL    decayWeights,    // TRUE if weight decay is active
  34.     NSFloat decayRate,    // Rate to decay weights if weight decay is active
  35.     NSFloat    momentum,    // Momentum rate for all weights
  36.     NSFloat    *delta        // Last weight Update
  37.     )
  38. {
  39.     register int i;
  40.  
  41.         NSFloat *mydata = (NSFloat *)getUserData(instance);
  42.         NSFloat up = getFloatParameter(instance, 0, 0);
  43.         NSFloat down = getFloatParameter(instance, 1, 0);
  44.         NSFloat cut = getFloatParameter(instance, 2, 0);
  45.         //NSFloat maxStep = getFloatParameter(instance, 0, 2);
  46.         //NSFloat minStep = getFloatParameter(instance, 2, 2);
  47.         NSFloat maxStep=1e15f;
  48.         NSFloat minStep=1e-15f;
  49.         NSFloat *lastGradient = mydata;
  50.         NSFloat *bestWeights  = mydata+length;
  51.  
  52.         if( !individual )
  53.         {
  54. //           MessageBox(NULL,"Must use individual steps for this gradient search",
  55. //                      "Error",MB_OK);
  56.            return;
  57.         }
  58.  
  59.         if( gEpoch < 0 )  // initialization
  60.         {
  61.            for (i=0; i<length; i++) {
  62.                  bestWeights[i] = weights[i];
  63.                  lastGradient[i] = 0.0f;
  64.            }
  65.         }
  66.  
  67.         if( ERR_GROW_MORE_THAN_ALLOWED )
  68.         {
  69.             for (i=0; i<length; i++) {
  70.                 step[i] *= cut;
  71.                 if(step[i]<minStep)
  72.                    step[i]=minStep;
  73.                 delta[i] = 0.0f;
  74.                 lastGradient[i] = 0.0f;
  75.                 weights[i] = bestWeights[i];
  76.             }
  77.         }
  78.         else
  79.         {
  80.             if( ERROR_DECREASED )
  81.             {
  82.               for (i=0; i<length; i++)
  83.                  bestWeights[i] = weights[i];
  84.             }
  85.         for (i=0; i<length; i++) {
  86.                 if(lastGradient[i]*gradient[i]>0)
  87.                 {
  88.                    step[i]=step[i]*up;
  89.                    if(step[i]>maxStep)
  90.                       step[i]=maxStep;
  91.                 }
  92.                 else if(lastGradient[i]*gradient[i]<0)
  93.                 {
  94.                    step[i]=step[i]*down;
  95.                    if(step[i]<minStep)
  96.                       step[i]=minStep;
  97.                 }
  98.                 delta[i] = momentum*delta[i] + (step[i]/(NSFloat)stepDivisor)*gradient[i];
  99.                 weights[i] += delta[i];
  100.                 lastGradient[i] = gradient[i];
  101.         }
  102.         }
  103. }
  104.  
  105. /******************************************/
  106. /* Management of instance data (OPTIONAL) */
  107.  
  108. __declspec(dllexport) DLLData *allocMomentum(
  109.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  110.     int    length,        // Length of the weight vector
  111.     BOOL    individual    // Indicates whether their is one learning rate for all weights (FALSE),
  112.                 // or each weight has its own learning rate
  113.     )
  114. {
  115.     DLLData *instance = allocDLLInstance(oldInstance);
  116.         NSFloat *mydata = (NSFloat *)calloc(length*2, sizeof(NSFloat));
  117.         setUserData(instance, mydata);
  118.         setParameterName(instance, 0, 0, "Up", FALSE);
  119.         setFloatParameter(instance, 0, 0, 1.1f, FALSE);
  120.         setParameterName(instance, 1, 0, "Down", FALSE);
  121.         setFloatParameter(instance, 1, 0, 0.9f, FALSE);
  122.         setParameterName(instance, 2, 0, "Cut", FALSE);
  123.         setFloatParameter(instance, 2, 0, 0.5f, FALSE);
  124.         //setParameterName(instance, 0, 1, "Max.Step", FALSE);
  125.         //setFloatParameter(instance, 0, 1, 1e15f, FALSE);
  126.         //setParameterName(instance, 1, 1, "Min.Step", FALSE);
  127.         //setFloatParameter(instance, 1, 1, 1e-15f, FALSE);
  128.     return instance;
  129. }
  130.  
  131. __declspec(dllexport) void freeMomentum(DLLData *instance)
  132. {
  133.         free(getUserData(instance));
  134.     freeDLLInstance(instance);
  135. }
  136.  
  137.  
  138. /********************************************************/
  139. /***************************/
  140. /* Activation of component */
  141. __declspec(dllexport) BOOL performPrePost(
  142.     DLLData    *instance,    // Pointer to instance data (may be NULL)
  143.     NSFloat    *input,     // Pointer to the input data
  144.     NSFloat    *output,     // Pointer to the output data
  145.     int rows,         // Number of rows of data
  146.     int cols,         // Number of cols of data
  147.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  148.     )
  149. {
  150.     int i, length=rows*cols;
  151.         NSFloat tolerance = getFloatParameter(instance, 0, 0);
  152.  
  153.         if( gEpoch < 0 ) // initialization
  154.         {
  155.            bestError = input[0];
  156.            for (i=0; i<length; i++)
  157.               output[i] = bestError;
  158.            return TRUE;
  159.         }
  160.         ERROR_DECREASED = 0;
  161.         ERR_GROW_MORE_THAN_ALLOWED = 0;
  162.         if( input[0] < bestError )
  163.         {
  164.               bestError = input[0];
  165.               ERROR_DECREASED = 1;
  166.         }
  167.         else if( input[0] > bestError*tolerance )
  168.         {
  169.               ERR_GROW_MORE_THAN_ALLOWED = 1;
  170.         }
  171.         for (i=0; i<length; i++)
  172.        output[i] = bestError;
  173.     return TRUE;    // Return whether to inject this sample or to call performPrePost with another sample
  174. }
  175.  
  176.  
  177. /******************************************/
  178. /* Management of instance data (OPTIONAL) */
  179.  
  180. __declspec(dllexport) DLLData *allocPrePost(
  181.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  182.     int *rows,         // Number of rows of output data, can be changed to reflect a diffenent number for the input data
  183.     int *cols,         // Number of cols of output data, can be changed to reflect a diffenent number for the input data
  184.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  185.     )
  186. {
  187.     DLLData *instance = allocDLLInstance(oldInstance);
  188.         setParameterName(instance, 0, 0, "Tolerance", FALSE);
  189.         setFloatParameter(instance, 0, 0, 1.01f, FALSE);
  190.     return instance;
  191. }
  192.  
  193. __declspec(dllexport) void freePrePost(DLLData *instance)
  194. {
  195.     freeDLLInstance(instance);
  196. }
  197.  
  198. __declspec(dllexport) void networkReset(DLLData *instance)
  199. {
  200.         gEpoch=-1;
  201.         ERROR_DECREASED = 0;
  202.         ERR_GROW_MORE_THAN_ALLOWED = 0;
  203. }
  204.  
  205. __declspec(dllexport) void epochEnded(DLLData *instance, int epoch)
  206. {
  207.     gEpoch=epoch;
  208. }
  209.  
  210.